/**
 * @file        CSafeSingleton.h
 * @brief       A Thread Safe Singleton template class for LCD App Framework
 * @author      Huabo Zheng
 * @date        Jan 11, 2012
 * @Copyright:  2012, www.netgear.com Inc. All rights reserved.
 */

#ifndef CSAFESINGLETON_H_
#define CSAFESINGLETON_H_

namespace NS_LCD_AFW
{

// T must be: no-throw default constructible and no-throw desconstructible
template<typename T>
class CSafeSingleton
{
private:
	struct tagObjectCreator
	{
		tagObjectCreator()
		{
			CSafeSingleton<T>::getInstance();
		}
		inline void doNothing() const
		{
		}
	};
	static tagObjectCreator m_createObject;

private:
	CSafeSingleton();

public:

	static T & getInstance()
	{
		static T obj;

		// The following line does nothing else than force the instantiation
		//  of CSafeSingleton<T>::m_createObject, whose constructor is
		//  called before main() begins.
		m_createObject.doNothing();

		return obj;
	}
};

template<typename T>
typename CSafeSingleton<T>::tagObjectCreator CSafeSingleton<T>::m_createObject;

} /* namespace NS_LCD_AFW */

#endif /* CSAFESINGLETON_H_ */

